<?php
namespace app\controllers\api\post;

use fate\http\Request;
use app\libs\Util;
use app\models\Posts;

class SearchController extends \fate\web\PureController {

    public function run() {
        $req = Request::getInstance();

        $k = Util::plainText($req->getParameter('k'));
        $pageSize = Util::plainText($req->getParameter('page_size'));

        if(!is_numeric($pageSize)) {
            $pageSize = 10;
        }

        $where = "status = 1 and instr(title, '{$k}')";
        $data = (new Posts())->getListByCondition(
            'id,title,post_time',
            $where,
            $pageSize);

        if(null === $data) {
            $this->ajaxReturn(null, 400001);
            exit;
        }

        $this->ajaxReturn([
            'data' => $data
        ]);
    }

}
